package com.example.a17376080_dlz_finaltest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView iv_main_change,iv_find_change,iv_world_change,iv_my_change;
    LinearLayout iv_homepage, iv_find, iv_world, iv_my;
    FragmentManager fm;
    FragmentTransaction ft;
    Fragment f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        intitButtonChange();
        //加载首页
        initMainPage();
        iv_main_change.setImageResource(R.mipmap.syyc);
        //toolbar.inflateMenu(R.menu.menu_major);
    }

    private void intitButtonChange() {
        iv_main_change.setImageResource(R.mipmap.syy);
        iv_find_change.setImageResource(R.mipmap.fx);
        iv_world_change.setImageResource(R.mipmap.lt);
        iv_my_change.setImageResource(R.mipmap.wd);
    }

    private void initMainPage(){
        fm=getSupportFragmentManager();
        ft=fm.beginTransaction();
        f=null;
        f=new Major_Fragment();
        ft.replace(R.id.fragment,f);
        ft.commit();
    }
    private void initView() {
        iv_homepage=findViewById(R.id.iv_homepage);
        iv_find=findViewById(R.id.iv_find);
        iv_world=findViewById(R.id.iv_world);
        iv_my=findViewById(R.id.iv_my);

        iv_main_change=findViewById(R.id.iv_main_change);
        iv_find_change=findViewById(R.id.iv_find_change);
        iv_world_change=findViewById(R.id.iv_world_change);
        iv_my_change=findViewById(R.id.iv_my_change);


        iv_homepage.setOnClickListener(this);
        iv_find.setOnClickListener(this);
        iv_world.setOnClickListener(this);
        iv_my.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
       fm=getSupportFragmentManager();
       ft=fm.beginTransaction();
       f=null;
        switch (v.getId()){
            case R.id.iv_homepage:
                f=new Major_Fragment();
                intitButtonChange();
                iv_main_change.setImageResource(R.mipmap.syyc);
                break;
            case R.id.iv_find:
                f=new Find_Fragment();
                intitButtonChange();
                iv_find_change.setImageResource(R.mipmap.fxc);
                break;
            case R.id.iv_world:
                f=new World_Fragment();
                intitButtonChange();
                iv_world_change.setImageResource(R.mipmap.ltc);
                break;
            case R.id.iv_my:
                f=new My_Fragment();
                intitButtonChange();
                iv_my_change.setImageResource(R.mipmap.wdc);
                break;
                default:
                    break;
        }
        ft.replace(R.id.fragment,f);
        ft.commit();
    }
}